package cn.zorbus.laian.interfaces.facade;

import cn.zorbus.laian.application.service.VideoService;
import cn.zorbus.laian.domain.repository.ISwitchRepository;
import cn.zorbus.laian.infrastructure.common.Result;
import cn.zorbus.laian.infrastructure.common.command.VideoChangeCommand;
import cn.zorbus.laian.infrastructure.common.command.VideoStatusCommand;
import cn.zorbus.laian.infrastructure.common.view.IDetailView;
import cn.zorbus.laian.infrastructure.common.view.VideoView;
import cn.zorbus.laian.infrastructure.config.GlobalFlag;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;
import java.util.Collection;

/**
 * create in 2023/12/4 01:45
 *
 * @author shishaodong
 * @version 0.0.1
 */
@Tag(name = "视频管理")
@Slf4j
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@CrossOrigin
public class VideoFacade {

    private final VideoService videoService;

    private final ISwitchRepository repository;

    private final GlobalFlag flag;

    @Operation(description = "获取所有的视频")
    @JsonView({IDetailView.class})
    @ResponseStatus(HttpStatus.OK)
    @GetMapping("/videos")
    public Result<Collection<VideoView>> findAllVideos() {
        return Result.success(videoService.findAll());
    }

    /**
     * findCover
     *
     * @param id String
     * @return Result
     */
    @Operation(description = "图片转成 base64")
    @GetMapping("/video/cover/{id}")
    public Result<String> findCover(@PathVariable String id) throws IOException {
        if (repository.ledClosed()) {
            return Result.failure("LED 关闭状态");
        }
        log.info("{}", id);
        String base64Image = Base64.getEncoder().encodeToString(resource.getInputStream().readAllBytes());
//        return Result.success(videoService.videoImage(id).base64Image());
        return Result.success("data:image/jpeg;base64," );
    }

    @Value("classpath:视频.webp")
    org.springframework.core.io.Resource resource;

    @Operation(description = "切换视频")
    @ResponseStatus(HttpStatus.ACCEPTED)
    @PutMapping("/video")
    public Result<String> change(@RequestBody VideoChangeCommand command) {
        if (repository.ledClosed()) {
            return Result.failure("LED 关闭状态");
        }
        videoService.videoChange(command);
        return Result.success("成功");
    }

    @Operation(description = "切换视频播放状态")
    @ResponseStatus(HttpStatus.ACCEPTED)
    @PutMapping("/video/status")
    public Result<String> status(@RequestBody VideoStatusCommand command) {
        if (repository.ledClosed()) {
            return Result.failure("LED 关闭状态");
        }
        videoService.videoStatus(command);
        return Result.success("成功");
    }
}
